#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from typing import List, Dict
from .base import FilterRule

class PunctuationChangeFilter(FilterRule):
    """标点符号变化过滤规则"""
    
    def __init__(self):
        # 构建对称的标点映射
        pairs = [
            ('。', '.'), ('，', ','), ('：', ':'), ('；', ';'), ('！', '!'), ('？', '?'),
            ('“', '"'), ('”', '"'), ('‘', "'"), ('’', "'")
        ]
        self.punctuation_set = set()
        self.punctuation_pairs = {}
        for a, b in pairs:
            self.punctuation_pairs[a] = b
            self.punctuation_pairs[b] = a
            self.punctuation_set.add(a)
            self.punctuation_set.add(b)
    
    def should_filter(self, add_content: str, remove_content: str, hunk_changes: List[Dict]) -> bool:
        """判断是否为标点符号变化"""
        add_clean = add_content.strip()
        remove_clean = remove_content.strip()
        
        if add_clean == remove_clean:
            return False
        
        if len(add_clean) == 1 and len(remove_clean) == 1:
            return (add_clean in self.punctuation_pairs and remove_clean == self.punctuation_pairs[add_clean]) or \
                   (remove_clean in self.punctuation_pairs and add_clean == self.punctuation_pairs[remove_clean])
        
        if self._is_end_punctuation_change(add_clean, remove_clean):
            return True
        
        if self._is_punctuation_replacement(add_clean, remove_clean):
            return True
        
        return False
    
    def _is_end_punctuation_change(self, add_content: str, remove_content: str) -> bool:
        """检查是否为末尾标点变化"""
        add_without_punct = add_content.rstrip('。.!?！？')
        remove_without_punct = remove_content.rstrip('。.!?！？')
        
        if add_without_punct == remove_without_punct:
            add_ends_with_punct = add_content != add_without_punct
            remove_ends_with_punct = remove_content != remove_without_punct
            return add_ends_with_punct or remove_ends_with_punct
        
        return False
    
    def _is_punctuation_replacement(self, add_content: str, remove_content: str) -> bool:
        """检查是否为标点符号替换"""
        if abs(len(add_content) - len(remove_content)) > 1:
            return False
        
        diff_count = 0
        min_len = min(len(add_content), len(remove_content))
        
        for i in range(min_len):
            if add_content[i] != remove_content[i]:
                if (add_content[i] in self.punctuation_pairs and remove_content[i] == self.punctuation_pairs[add_content[i]]) or \
                   (remove_content[i] in self.punctuation_pairs and add_content[i] == self.punctuation_pairs[remove_content[i]]):
                    diff_count += 1
                else:
                    return False
        
        if len(add_content) > len(remove_content):
            extra_char = add_content[min_len]
            return extra_char in '。.!?！？' and diff_count == 0
        elif len(remove_content) > len(add_content):
            extra_char = remove_content[min_len]
            return extra_char in '。.!?！？' and diff_count == 0
        
        return diff_count == 1
    
    def get_filter_reason(self) -> str:
        return "punctuation_change" 